<?php
/**
 * 插件管理Model类
 * @file			PluginsModel.class.php
 * @author		kelecode@gmail.com
 * @date       	2014-03-09
 * modify
 */
class PluginsModel extends Model{

	private $table = array();
	public $pageSize;

	public function __construct(){
		parent::__construct();

		$this->table = array(
			'options' => C('db_table_prefix') . 'options',
			'plugins' => C('db_table_prefix') . 'plugins',
		);
		$this->pageSize = C('page.page_size');
	}

	/**
	 * 插件列表
	 * @param string $type 可取值 all, active, deactive
	 * @return array
	 */
	public function pluginList($type='all'){
		$pageSize = $this->pageSize;
		$currentPage = (int)$_GET[C('page.var_page')];
		$currentPage = $currentPage <= 0 ? 1 : $currentPage;
		$start = ($currentPage - 1) * $pageSize;
        $start = $start < 0 ? 0 : $start;
		
		$count = 0;
		$lists = array();
		$plugins = array();
		$all_plugins = load_plugin(true);
		$actived_plugin = $this->getActivedPlugin();
		foreach($all_plugins as $k => $path){
			$info = $this->get_plugin_info($path);
			$info['is_active'] = (int)in_array($info['folder'], $actived_plugin);
			$lists[$k] = $info;
		}
		
		if($type == 'all'){
			$plugins = $lists;
		}else if($type == 'active' || $type == 'deactive'){
			foreach($lists as $key => $val){
				if( ($type == 'active' && $val['is_active'] == 1) || ($type == 'deactive' && $val['is_active'] == 0) ){
					$plugins[] = $val;
				}
			}
		}

		return array('count' => count($lists),'lists' => $plugins);
	}
	
	/**
	 * 获取插件信息
	 * @param string $path
	 * @return array $info
	 */
	public function get_plugin_info($path){
		if( !is_dir($path) ){
			return false;
		}
		$info = array();
		$info['folder'] = basename($path);
		$file = $path . '/app.php';
		$content = @file_get_contents($file);
		if( !trim($content) ){
			return false;
		}
		$temp = explode('***/', $content);
		$temp = explode('/***', $temp[0]);
		$temp = explode("\n", $temp[1]);
		foreach($temp as $val){
			$val = trim($val);
			if( !empty($val) ){
				$t = explode("\t",$val);
				if( is_array($t) && !empty($t) ){
					$info[trim($t[0])] = trim($t[1]);
				}
			}
		}
		
		return $info;	
	}
	
	/**
	 * 获取激活的插件
	 * @return array
	 */
	public function getActivedPlugin(){
		$sql = "SELECT `option_value` FROM `{$this->table['options']}` WHERE `option_name`='active_plugins'";
		$result = $this->query($sql);
		$plugins = empty($result[0]['option_value']) ? array() : unserialize($result[0]['option_value']);

		return $plugins;
	}

	/**
	 * 更新状态
	 * @return boolean
	 */
	public function updateStatus($names, $status){
		if(!is_array($names) || empty($names)){
			return false;
		}
		$status = !in_array($status,array(0,1)) ? 1 : $status;
		$utime = time();
		$active_plugin = $this->getActivedPlugin();
		if($status == 1){
			$option_value = array_merge($active_plugin, $names);
		}else{
			$option_value = array_diff($active_plugin,  $names);
		}
		$option_value = serialize( array_unique($option_value) );
		$sql = "UPDATE `{$this->table['options']}` SET `option_value`='{$option_value}', `utime`='{$utime}' WHERE `option_name`='active_plugins' LIMIT 1";
		$result = $this->execute($sql);

		return $result;
	}

	public function uninstall(){
		
	}

}